package com.kye.express.business.deposit.presenter;

import com.kye.express.contract.base.BasePresenter;
import com.kye.express.contract.base.LifecycleOpenApiResultObserver;
import com.kye.express.contract.base.view.IView;
import com.kye.model.UserSession;
import com.kye.model.bean.OpenApiResult;
import com.kye.model.bean.deposit.RequestIsNeedTicket;
import com.kye.model.bean.deposit.RequestQueryReserveBalance;
import com.kye.model.bean.deposit.ResultIsNeedTicketBean;
import com.kye.model.bean.deposit.ResultQueryReserveBalanceBean;
import com.kye.model.bean.user.User;
import com.kye.model.datasource.DepositDataSource;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * 预存运费 Presenter
 * <p>
 * Created by SkyYu on 2018/1/16
 */
public interface DepositContract {

    interface DepositView extends IView {
        void onLoadUser(User user);

        void onLoadBalanceSuccess(OpenApiResult<ResultQueryReserveBalanceBean> result);

        void onLoadBalanceApiError(OpenApiResult<ResultQueryReserveBalanceBean> result, int code, String msg);

        void onLoadBalanceOtherError(Throwable e);

        void onLoadIsNeedTicketSuccess(OpenApiResult<ResultIsNeedTicketBean> result);

        void onLoadIsNeedTicketApiError(OpenApiResult<ResultIsNeedTicketBean> result, int code, String msg);

        void onLoadIsNeedTicketOtherError(Throwable e);
    }

    class DepositPresenter extends BasePresenter<DepositView> {

        DepositDataSource depositDataSource;

        public DepositPresenter() {
            depositDataSource = new DepositDataSource();
        }

        @Override
        public void bind(DepositView view) {
            super.bind(view);
            UserSession.getInstance()
                    .getUserForRx()
                    .compose(getView().bindToLifecycleDestroy())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe((user) ->
                            getView().onLoadUser(user)
                    );
        }

        public void requestToQueryReserveBalance(String phone, String companyCode) {
            depositDataSource.queryReserveBalance(new RequestQueryReserveBalance(phone, companyCode))
                    .compose(getView().bindToLifecycleDestroy())
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new LifecycleOpenApiResultObserver<ResultQueryReserveBalanceBean>(this) {

                        @Override
                        public void onStart(Disposable d) {

                        }

                        @Override
                        public void onLoaded() {

                        }

                        @Override
                        public void onSuccess(OpenApiResult<ResultQueryReserveBalanceBean> result, ResultQueryReserveBalanceBean helpInfoListModelList) {
                            getView().onLoadBalanceSuccess(result);
                        }

                        @Override
                        public void onApiError(OpenApiResult<ResultQueryReserveBalanceBean> result, int code, String msg) {
                            getView().onLoadBalanceApiError(result, code, msg);
                        }

                        @Override
                        public void onOtherError(Throwable e) {
                            getView().onLoadBalanceOtherError(e);
                        }
                    });
        }

        public void requestToIsNeedTicket(String phone, String companyCode) {
            depositDataSource.isNeedTicket(new RequestIsNeedTicket(phone, companyCode))
                    .compose(getView().bindToLifecycleDestroy())
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new LifecycleOpenApiResultObserver<ResultIsNeedTicketBean>(this) {
                        @Override
                        public void onStart(Disposable d) {

                        }

                        @Override
                        public void onLoaded() {

                        }

                        @Override
                        public void onSuccess(OpenApiResult<ResultIsNeedTicketBean> result, ResultIsNeedTicketBean helpInfoListModelList) {
                            getView().onLoadIsNeedTicketSuccess(result);
                        }

                        @Override
                        public void onApiError(OpenApiResult<ResultIsNeedTicketBean> result, int code, String msg) {
                            getView().onLoadIsNeedTicketApiError(result, code, msg);
                        }

                        @Override
                        public void onOtherError(Throwable e) {
                            getView().onLoadIsNeedTicketOtherError(e);
                        }
                    });
        }
    }
}